<template>
  <el-dialog :title="title" :visible="visible" @close="visible=false">
    <el-form
      ref="form"
      :model="entityData"
      :rules="rules"
      label-width="80px"
      label-position="right"
      style="width:90%;margin:0px auto;"
    >
      <!--表单区域 -->
      <el-form-item label="上级" prop="parentId">
        <el-cascader
          v-model="selectedParentData"
          placeholder="请选择"
          :options="parentTreeData"
          :props="cascaderProps"
          expand-trigger="click"
          change-on-select
          :show-all-levels="false"
          @visible-change="parentChanged"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="entityData.name" :readonly="readonly" />
      </el-form-item>
      <el-form-item label="排序号" prop="orderNo">
        <el-input v-model="entityData.orderNo" :readonly="readonly" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-show="saveButtonVisible" type="primary" @click="save">保存</el-button>
      <el-button @click="close">关闭</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { detailMixin } from '@/mixin/detailMixin'
import { getTreeDeepArr } from '@/utils/tree'
const MODULE_CODE = 'support'
const ENTITY_TYPE = 'contentTemplateCategory'
export default {
  name: ENTITY_TYPE + '-detail',
  mixins: [detailMixin],
  props: {
    parentId: {
      type: String,
      default: '',
      required: false
    },
    parentName: {
      type: String,
      default: '',
      required: false
    }
  },
  data() {
    return {
      entityType: ENTITY_TYPE,
      moduleCode: MODULE_CODE,
      // eslint-disable-next-line no-eval
      api: eval('this.$api.' + MODULE_CODE + '.' + ENTITY_TYPE),
      pageCode: MODULE_CODE + ':' + ENTITY_TYPE + ':',
      entityData: {

      },
      rules: {
        // 前端验证规则
        parentId: [
          { required: true, message: '请选择上级', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      },
      // 上级树
      parentTreeData: [],
      selectedParentData: '',
      cascaderProps: {
        value: 'id'
      }
    }
  },
  methods: {

    afterAdd() {
      this.initParentTree()
    },
    afterModify() {
      this.initParentTree()
    },
    initParentTree() {
      this.$api.support.contentTemplateCategory.tree()
        .then(res => {
          this.parentTreeData = res.data
          this.selectedParentData = getTreeDeepArr(this.entityData.parentId, this.parentTreeData)
        })
    },
    parentChanged(visible) {
      if (!visible && this.selectedParentData && this.selectedParentData.length > 0) {
        this.entityData.parentId = this.selectedParentData[this.selectedParentData.length - 1]
      }
    }

  }

}
</script>

<style lang="scss">
</style>
